package loader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import machineLearning.SentenceExampleBuilder;
import machineLearning.SentenceExampleBuilder.semAllEmotions;
import machineLearning.SentenceExampleBuilder.semEmotions;

import rainbownlp.core.Artifact;
import rainbownlp.core.Setting;
import rainbownlp.machineLearning.MLExample;
import rainbownlp.util.FileUtil;

public class loadSemEvalAnnotations {
	public static void main (String[] args)
	{
		String input_text_file_test = tools.Configuration.getValue("ProjectDataFilesRoot")+
		"/AffectiveText.trial/affectivetext_trial.emotions.gold";
		String input_text_file_train = tools.Configuration.getValue("ProjectDataFilesRoot")+
		"/AffectiveText.test/affectivetext_test.emotions.gold";


		Setting.TrainingMode = true;
		if(args.length>1 && args[1].equals("test"))
			Setting.TrainingMode = false;
		
		if (Setting.TrainingMode ==true)
		{
			try {
				loadSemEvalAnnotations(input_text_file_train);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else
		{
			try {
				loadSemEvalAnnotations(input_text_file_test);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	public static void loadSemEvalAnnotations(String input_text_file)
	{
		List<String> lines = FileUtil.loadLineByLine(input_text_file);
		int counter = 0;
//		1 22 2 60 0 64 0
//		id  anger disgust fear joy sadness surprise 
	    for (String line: lines)
	    {
	
	    	String sent_id = line.split(" ")[0];
	    	ArrayList<semAllEmotions> emotions = getExistingEmotions(line);
	    	for (semAllEmotions emotion:emotions)
	    	{
	    		semAllEmotions rel_neg_emotion = 
	    			semAllEmotions.valueOf("NOT_"+emotion.toString());
	    		
	    		//get related example
	    		MLExample rel_example = MLExample.findInstance
	    			(sent_id, SentenceExampleBuilder.experimentGroup, rel_neg_emotion.ordinal());
	    		//set expected
	    		rel_example.setExpectedClass(emotion.ordinal());
	    		//save
	    		MLExample.saveExample(rel_example);
	    		
	    	}
	    	counter++;
	    	System.out.println("processed"+counter+"/"+lines.size());
	     }
		
	}
	public static ArrayList<semAllEmotions> getExistingEmotions(String line)
	{
		ArrayList<semAllEmotions> existingEmotions = new ArrayList<semAllEmotions>();
//		id  anger disgust fear joy sadness surprise 
		String[] scores = line.split(" ");
		for(int i=0;i<scores.length;i++)
		{
			
		}
		int anger = Integer.parseInt(scores[1]);
		int disgust = Integer.parseInt(scores[2]);
		int fear = Integer.parseInt(scores[3]);
		int joy = Integer.parseInt(scores[4]);
		int sadness = Integer.parseInt(scores[5]);
		int surprise = Integer.parseInt(scores[6]);
		
		int thershold =25;
		if (anger>thershold)
		{
			existingEmotions.add(semAllEmotions.ANGER);
		}
		if (disgust>thershold)
		{
			existingEmotions.add(semAllEmotions.DISGUST);
		}
		if (fear>thershold)
		{
			existingEmotions.add(semAllEmotions.FEAR);
		}
		if (joy>thershold)
		{
			existingEmotions.add(semAllEmotions.JOY);
		}
		if (sadness>thershold)
		{
			existingEmotions.add(semAllEmotions.SADNESS);
		}
		if (surprise>thershold)
		{
			existingEmotions.add(semAllEmotions.SURPRISE);
		}
		return existingEmotions;
	}
}
